import _Pan3D.role.MeshDataManager;
import _Pan3D.texture.TextureManager;

import _me.Config;
import _me.Scene_data;

import content.SceneLevel;

import flash.display3D.Context3D;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filesystem.File;
import flash.utils.setTimeout;

import models.ModelLocator;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.core.Application;
import mx.core.UIComponent;
import mx.events.FlexNativeWindowBoundsEvent;

import util.FileUtils;

[Bindable]
private var ary:ArrayCollection = new ArrayCollection([1,3,4]);
private var context3D:Context3D;
private var model:ModelLocator = ModelLocator.getInstance();
public static var instance:BoneMcApp;
public function inits():void
{
	if(stage){
		addToStage();
	}else{
		this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
	}
	instance = this;
	//trace(File.desktopDirectory.url);
}

private function addToStage(event:Event=null):void{
	stage.scaleMode = StageScaleMode.NO_SCALE;
	stage.align = StageAlign.TOP_LEFT;
	initScene();
	
//	stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, onContext3DCreate);
//	stage.stage3Ds[0].requestContext3D();
	
}

private function onContext3DCreate(event:Event):void
{
	var t:Stage3D = event.target as Stage3D;
	context3D = t.context3D;
	if (context3D == null)
	{
		// Currently no 3d context is available (error!)
		trace('ERROR: no context3D - video driver problem?');
		return;
	}
	trace(context3D.driverInfo);
	context3D.enableErrorChecking = true;
	//if(stage.width == 0){
	context3D.configureBackBuffer(stage.stageWidth,stage.stageHeight, 2, true);
	
	
	
	initScene();
}

private function renderScene(event:Event=null):void{
	//viewmatrix.identity();
//	this.context3D.clear(0.8,0.8,0.8);
	
//	this.context3D.present();
	_sceneView.upData();
}


protected function windowResizeHandler(event:FlexNativeWindowBoundsEvent):void
{
	context3D.configureBackBuffer(stage.stageWidth,stage.stageHeight, 2, true);
	
	bonePanle.x = stage.stageWidth - bonePanle.width - 5;
	meshPanle.x = stage.stageWidth - meshPanle.width - 5;
}

/*public function initData():void{
	var obj:Object = FileUtils.readFile(File.documentsDirectory.url + "/bone/lib.txt");
	if(obj){
		model.libData = ArrayCollection(obj);
		if(model.libData.length){
			model.currentLibName = model.libData[0].name;
		}
	}
	//bonePanle.initList();
	//meshPanle.initList();
}*/
private function ontextSizeError(event:Event):void{
	Alert.show("贴图文件的尺寸必须2的幂数，比如2,4,8,16....","警告");
}

private function initScene():void{
	Scene_data.stage=this.stage;
	Scene_data.root=this;
	Scene_data.fileRoot="_File/nijia2/";
	
	Config.init(stage, _config_end);
}
private var _sceneView:SceneLevel;
private function _config_end():void
{
	this.context3D = Scene_data.context3D;
	MeshDataManager.getInstance().init(this.context3D);
	TextureManager.getInstance().init(this.context3D);
	TextureManager.getInstance().addEventListener("sizeError",ontextSizeError);
	//initData();
	
	_sceneView=new SceneLevel;
	_sceneView.initData();
	
	//setTimeout(startRender,10000);
	startRender();
}
private function startRender():void{
	this.addEventListener(Event.ENTER_FRAME,renderScene);
}
public function set titleName(value:String):void{
	this.title = "动画编辑器" + value;
}









